package com.jketing.entity.ams.base;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.Inheritance;
import javax.persistence.InheritanceType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.OrderBy;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.persistence.Transient;

import org.springframework.format.annotation.DateTimeFormat;

import com.jketing.annotation.FieldMeta;
import com.jketing.entity.BaseEntity;
import com.jketing.entity.ams.sys.Dictionary;
import com.jketing.entity.ams.sys.Account;

/**
 * Person 人员实体基类
 * 
 * @author  李荣华
 * @version v1.0
 * @date     2018-08-13
 * @since   Jpa 2.1
 */
@Entity
@Table(name = "Person_")
@Inheritance(strategy=InheritanceType.JOINED)
public class Person extends BaseEntity {
	private static final long serialVersionUID = -5924384443903290095L;
	/**
	 * name:姓名
	 */
	@FieldMeta(name="String:name",explain="姓名") 
	protected String name;
	/**
	 * sex:性别
	 */
	@FieldMeta(name="Entity:sex",explain="性别")
	protected Dictionary sex;
	/**
	 * nation:民族
	 */
	@FieldMeta(name="Entity:nation",explain="民族")
	protected Dictionary nation;
	/**
	 * age:年龄
	 */
	protected Integer age;
	/**
	 * cardType:证件类型
	 */
	protected Dictionary cardType;
	/**
	 * cardId:证件号码
	 */
	@FieldMeta(name="String:cardId",explain="证件号码")
	protected String cardId;
	/**
	 * birthday:出生日期
	 */
	@FieldMeta(name="Date:birthday",explain="出生日期")
	protected Date birthday;
	/**
	 * remark:备注
	 */
	protected String remark;
	/**
	 * accounts:账号集合
	 */
	@FieldMeta(name="Entity:accounts",explain="账号集合")
	protected Set<Account> accounts = new HashSet<Account>();
	
	/**
	 * 员工姓名
	 */
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}

	/**
	 * 员工性别
	 */
	@ManyToOne
	@JoinColumn(name = "SexId")
	public Dictionary getSex() {
		return sex;
	}
	public void setSex(Dictionary sex) {
		this.sex = sex;
	}
	
	/**
	 * 民族
	 */
	@ManyToOne
	@JoinColumn(name = "NationId")
	public Dictionary getNation() {
		return nation;
	}
	public void setNation(Dictionary nation) {
		this.nation = nation;
	}
	
	/**
	 * age:年龄
	 * 
	 * 不存数据库，按出生日期计算年龄
	 * 
	 */
	@Transient
	public Integer getAge() {
		return age;
	}
	public void setAge(Integer age) {
		this.age = age;
	}
	
	/**
	 * cardType 证件类型
	 */
	@ManyToOne
	@JoinColumn(name = "CardTypeId")
	public Dictionary getCardType() {
		return cardType;
	}
	public void setCardType(Dictionary cardType) {
		this.cardType = cardType;
	}
	
	/**
	 * cardId 证件号码
	 */
	public String getCardId() {
		return cardId;
	}
	public void setCardId(String cardId) {
		this.cardId = cardId;
	}
	
	/**
	 * 出生日期
	 */
	@DateTimeFormat(pattern="yyyy-MM-dd")
	@Temporal(value = TemporalType.DATE)
	public Date getBirthday() {
		return birthday;
	}	
	public void setBirthday(Date birthday) {
		this.birthday = birthday;
	}
	
	/**
	 * remark 员工信息备注
	 */
	public String getRemark() {
		return remark;
	}
	
	public void setRemark(String remark) {
		this.remark = remark;
	}
	
	/**
	 * 拥有的系统登录账号集合，既系统用户
	 */
	@OneToMany(mappedBy = "person", cascade = CascadeType.ALL, fetch = FetchType.LAZY)
	@OrderBy(value = "id ASC")
	public Set<Account> getAccounts() {
		return accounts;
	}
	public void setAccounts(Set<Account> accounts) {
		this.accounts = accounts;
	}
	
	/**
	 * 增加账号
	 * 
	 * @param entity
	 */
	public void add(Account entity) {
		if (!accounts.contains(entity)) {
			entity.setPerson(this);
			accounts.add(entity);
		}
	}	
	
	
	/**
	 * 删除账号
	 * 
	 * @param entity
	 */
	public void remove(Account entity) {
		if (accounts.contains(entity)) {
			entity.setPerson(null);
			accounts.remove(entity);
		}
	}
	
	/**
	 * 	equals 判断实例是否相等
	 * 
	 * 	是本类实例并且实例id值相等返回true
	 * 	其它情况返回false
	 * 
	 * 	@param object 其它实例
	 */
	@Override
	public boolean equals(Object object) {
		if (!(object instanceof Person)) {
			return false;
		}
		Person other = (Person) object;
		if(!id.equals(other.id)){
			return false;
		}
		return true;
	}
}
